// ------------------------------------------------------------------------------------------------
// File   : DX9AfterEffect.cpp
// Date   : 2012-08-01
// Author : Weng xiiao yi
// Descr. : The parent of all after effects
//
// ------------------------------------------------------------------------------------------------

// ------------------------------------------------------------------------------------------------
// HEADERS
// ------------------------------------------------------------------------------------------------
#include "DX9AfterEffect.h" 
#include "DX9AfterEffectManager.h"

// ------------------------------------------------------------------------------------------------
// Global - DX9AfterEffect
// ------------------------------------------------------------------------------------------------
D3DVERTEXELEMENT9 g_AfterEffectDecl[] =
{
	{ 0, 0,  D3DDECLTYPE_FLOAT4,	D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION,		0 },
	D3DDECL_END()
};

// ------------------------------------------------------------------------------------------------
// IMPLEMENTATION - DX9AfterEffect
// ------------------------------------------------------------------------------------------------

DX9AfterEffect::DX9AfterEffect()
{ 
	m_bEnabled[eDX9_AE_Context_Read]  = FALSE;
	m_bEnabled[eDX9_AE_Context_Write] = FALSE;
}

DX9AfterEffect::~DX9AfterEffect()
{ 
	DX9_SAFE_RELEASE( m_pAfterEffectDeclVD );
	DX9_SAFE_RELEASE( m_pQuadVB );
}

void DX9AfterEffect::OnDeviceLost()
{
	DX9_SAFE_RELEASE( m_pAfterEffectDeclVD );
	DX9_SAFE_RELEASE( m_pQuadVB );
}

void DX9AfterEffect::OnDeviceReset()
{

	m_QuadVert[0].p.Set( -1.0f, -1.0f, 0.0f, 1.0f );
	m_QuadVert[1].p.Set( -1.0f, 1.0f, 0.0f, 1.0f );
	m_QuadVert[2].p.Set( 1.0f, -1.0f, 0.0f, 1.0f );
	m_QuadVert[3].p.Set( 1.0f, 1.0f, 0.0f, 1.0f );


	LPDIRECT3DDEVICE9 pD3DDev = DX9GetAfterEffectManager()->m_pDevice;

	HRESULT hr = pD3DDev->CreateVertexDeclaration( g_AfterEffectDecl, &m_pAfterEffectDeclVD );
	DX9Verify(SUCCEEDED(hr));

	hr = pD3DDev->CreateVertexBuffer( 4 * sizeof(AfterEffectQuadVertex), D3DUSAGE_WRITEONLY, 0,
		D3DPOOL_MANAGED, &m_pQuadVB, NULL);
	void *pVert = NULL;
	m_pQuadVB->Lock( 0, sizeof(m_QuadVert), (void**)&pVert, 0 );
	memcpy( pVert, m_QuadVert, sizeof(m_QuadVert) );
	m_pQuadVB->Unlock();
}

void DX9AfterEffect::Enable(DX9_AE_Context _eContext, BOOL _bEnable) 
{
	DX9Verify( _eContext < eDX9_AE_Context_Num );
	m_bEnabled[_eContext] = _bEnable; 
}
